<?php
namespace Models\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Predicate;
use Zend\Db\Sql\Expression;

class DiscountsTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	public function saveDiscounts($morderid,$discount)
    {
		if($discount!=""){
			$data = array(
				'discount' 					=> $discount,
				'user_medicine_order_id' 	=> $morderid,	
				'admin_paid_status' 	 	=> 0,   
				'added_date'	 			=> date('Y-m-d H:i:s'), 				
				'updated_date'	 			=> date('Y-m-d H:i:s'), 				
				'status'	 				=> 0, 				
			);	
			$insertresult=$this->tableGateway->insert($data);	
			return $insertresult;
		}else{
			return 0;
		}
    }
	public function deleteDiscounts($mOrderId)
   {
		$row=$this->tableGateway->delete(array('(user_medicine_order_id IN ('.$mOrderId.'))'));
		return $row;
   }
	public function userDiscounts($user_id){
		$select = $this->tableGateway->getSql()->select();
		$select->join('user_medicine_order', new Expression('user_medicine_order.user_medicine_order_id=discounts.user_medicine_order_id'),array('user_id','prescription_details','prescription_upload_path'),'left');
		$select->join('order_delivery_details', new Expression('discounts.user_medicine_order_id=order_delivery_details.user_medicine_order_id'),array('price','delivered_date'),'left');
		$select->join('user', new Expression('user.user_id=order_delivery_details.shop_id'),array('shop_id'=>'user_id','shop_name'=>'user_name'),'left');
		$select->join('user_details', new Expression('user_details.user_id=user.user_id'),array('address','phone','location','pincode'),'left');	
		$select->where('user_medicine_order.user_id="'.$user_id.'"');
		$select->where('user_medicine_order.status="2"');		
		$select->where('order_delivery_details.shop_order_status="2"');
		$select->order('discounts.discount_id DESC');
		$resultSet = $this->tableGateway->selectWith($select);		
		return $resultSet;
	}
	public function getAllDiscounts(){
		$select = $this->tableGateway->getSql()->select();
		$select->join('user_medicine_order', new Expression('user_medicine_order.user_medicine_order_id=discounts.user_medicine_order_id'),array('user_id','prescription_details','prescription_upload_path'),'left');
		$select->join('order_delivery_details', new Expression('discounts.user_medicine_order_id=order_delivery_details.user_medicine_order_id'),array('price','delivered_date'),'left');
		$select->join('user', new Expression('user.user_id=order_delivery_details.shop_id'),array('shop_id'=>'user_id','shop_name'=>'user_name'),'left');
		$select->join('user_details', new Expression('user_details.user_id=user.user_id'),array('address','phone','location','pincode'),'left');
		$select->where('order_delivery_details.shop_order_status="2"');		
		$select->where('user_medicine_order.status="2"');	
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
	public function getShopDiscounts($shop_id){
		$select = $this->tableGateway->getSql()->select();
		$select->join('user_medicine_order', new Expression('user_medicine_order.user_medicine_order_id=discounts.user_medicine_order_id'),array('user_id'),'left');
		$select->join('order_delivery_details', new Expression('discounts.user_medicine_order_id=order_delivery_details.user_medicine_order_id'),array('price','delivered_date'),'left');
		$select->join('user', new Expression('user.user_id=user_medicine_order.user_id'),array('user_id','user_name'),'left');
		$select->join('user_details', new Expression('user_details.user_id=user.user_id'),array('city','state','country','address'),'left');	
		$select->where('order_delivery_details.shop_id="'.$shop_id.'"');
		$select->where('user_medicine_order.status="2"');
		$select->order('discounts.discount_id DESC');
		$resultSet = $this->tableGateway->selectWith($select);		
		return $resultSet;
	}

	public function paidDiscount($tracking_id){
		$data = array(
			'admin_paid_status'  => 1, 
			'updated_date'	     => date('Y-m-d H:i:s'),
		);
		$updaed_status=$this->tableGateway->update($data, array('user_medicine_order_id' => $tracking_id));
		return 	$updaed_status;
	}
	public function getPaidAndUnPaidDiscounts($shop_id,$type){
		$select = $this->tableGateway->getSql()->select();
		$select->join('order_delivery_details', new Expression('discounts.user_medicine_order_id=order_delivery_details.user_medicine_order_id'),array('*'),'left');
		if($type=='un_paid'){
			$select->where('discounts.admin_paid_status="0"');
		}elseif($type=='paid'){
			$select->where('discounts.admin_paid_status="1"');
		}
		$select->where('order_delivery_details.shop_id="'.$shop_id.'"');		
		$resultSet = $this->tableGateway->selectWith($select);		
		return $resultSet->count();
	}
}